package com.i4season.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.example.i4seasoncameralib.cameramanager.CameraManager;
import com.example.i4seasoncameralib.cameramanager.handler.CameraConstant;
import com.example.i4seasoncameralib.cameramanager.handler.CameraEventObserver;
import com.jni.logmanageOtg.LogManagerWD;

public class NativeOtgCameraActivity extends FragmentActivity implements View.OnClickListener {

    private ImageView mCameraImage;
    private final String TAG = "OTGCameraActivity";

    /**
     * OTG 专用
     */
    private TextView mPhoto;
    private TextView mRecoder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_native_otgcamera);

        initView();
        initData();
        initListener();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        int deviceOnlineStatus = CameraManager.getInstance().getDeviceOnlineStatus();
        if (deviceOnlineStatus == CameraConstant.DEVICE_OFFLINE) {
            CameraManager.getInstance().checkOtgCamera(this);
        }
    }

    /**
     * 初始化view
     */
    private void initView() {
        mCameraImage = findViewById(R.id.app_camera_image);

        mPhoto = findViewById(R.id.app_photo);
        mRecoder = findViewById(R.id.app_recoder);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        initCamera();

        int deviceOnlineStatus = CameraManager.getInstance().getDeviceOnlineStatus();
        if (deviceOnlineStatus == CameraConstant.DEVICE_ONLINE) {
            Log.d(TAG, "deviceOnlineStatus = " + deviceOnlineStatus);
        }
    }

    /**
     * 初始化数据
     */
    private void initCamera() {
        //camera init
        CameraConstant.SAVE_SELF = false;
        CameraConstant.SAVE_CIRCLE = false;
        CameraConstant.app_album_name = "/OTGCamearSDKDemo";

        String absolutePath = getApplicationContext()
                .getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
        LogManagerWD.APP_SDCARD = absolutePath;

        CameraManager.getInstance().initProgrammeSdk(getApplicationContext());

        CameraManager.getInstance().setOnOfflineListener(availableListener);
        CameraManager.getInstance().checkOtgCamera(this);
        int deviceOnlineStatus = CameraManager.getInstance().getDeviceOnlineStatus();
    }

    /**
     * 设置监听
     */
    private void initListener() {
        mCameraImage.setOnClickListener(this);
        mPhoto.setOnClickListener(this);
        mRecoder.setOnClickListener(this);
        CameraManager.getInstance().setOnAcceptBitmapListener(acceptBmpListener, false);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.app_photo) {
            CameraManager.getInstance().takePhoto(mCurrentBitmap, mediaListener);
        } else if (id == R.id.app_recoder) {//录制显示计时时间需要自行定时器显示
            String recorderText = mRecoder.getText().toString();
            if (recorderText.equals("录制")) {
                mRecoder.setText("停止录制");
                mRecoder.setTextColor(Color.RED);
                CameraManager.getInstance().startVideoRecoder();
            } else {
                mRecoder.setText("录制");
                mRecoder.setTextColor(Color.WHITE);
                CameraManager.getInstance().stopVideoRecoder(mediaListener);
            }
        }
    }

    private Bitmap mCurrentBitmap;

    /***/
    private final CameraEventObserver.OnAcceptBitmapListener acceptBmpListener = bitmap -> runOnUiThread(() -> {
        mCurrentBitmap = bitmap;
        mCameraImage.setImageBitmap(bitmap);
    });

    /**
     * 拍照或者录像
     */
    private final CameraEventObserver.OnTakePhotoOrRecoderListener mediaListener = (isSuccessful, savePath, takeType) -> {
        Log.d(TAG, ">>> onTakePhotoOrRecoderListener : " + isSuccessful + " ," + savePath + " ," + takeType);
        runOnUiThread(() -> {
            if (isSuccessful) {
                Toast.makeText(NativeOtgCameraActivity.this, "成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(NativeOtgCameraActivity.this, "失败", Toast.LENGTH_SHORT).show();
            }
        });
    };

    /**
     * 设备是否在线
     */
    private final CameraEventObserver.OfflineListener availableListener = new CameraEventObserver.OfflineListener() {
        @Override
        public void onOfflineListener() {
            Log.d(TAG, "设备离线");
            runOnUiThread(() -> Toast.makeText(NativeOtgCameraActivity.this, "设备离线", Toast.LENGTH_SHORT).show());
        }

        @Override
        public void onConnectingListener() {
            Log.d(TAG, "onConnectingListener");
        }

        @Override
        public void onConnectErrorListener(int error) {
            Log.d(TAG, "连接错误:");
        }

        @Override
        public void onOnlineListener() {
            runOnUiThread(() -> Toast.makeText(NativeOtgCameraActivity.this, "设备上线", Toast.LENGTH_SHORT).show());
        }

        @Override
        public void onUpdataDeviceListener() {
        }
    };

    /**
     * 设备信息
     */
    private final CameraEventObserver.OnAcceptFwInfoListener deviceInfoListener = (isSuccessful, cameraFirmInfo) -> {
        String getvendor = cameraFirmInfo.getvendor();
    };

    @Override
    protected void onDestroy() {
        CameraManager.getInstance().removeEventObserverListenser(CameraConstant.EVENT_BITMAP, acceptBmpListener);
        CameraManager.getInstance().removeEventObserverListenser(CameraConstant.EVENT_TAKE_PHOTO_RECORDERO, mediaListener);
        CameraManager.getInstance().removeEventObserverListenser(CameraConstant.EVENT_ONLINE_OFFLINE_CHANGE, availableListener);
        //CameraManager.getInstance().removeEventObserverListenser(CameraConstant.EVENT_DEVICE_INFO_CHANGE, deviceInfoListener/*CameraEventObserver.OnWifiCameraInfoListener*/);//注意类型转换
        super.onDestroy();
    }
}